package com.xiaofeng.auth.domain.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xiaofeng.auth.common.enums.IsDeletedFlagEnum;
import com.xiaofeng.auth.domain.convert.AuthPermissionBOConverter;
import com.xiaofeng.auth.domain.convert.AuthRoleBOConverter;
import com.xiaofeng.auth.domain.entity.AuthPermissionBO;
import com.xiaofeng.auth.domain.redis.RedisUtil;
import com.xiaofeng.auth.domain.service.AuthPermissionDomainService;
import com.xiaofeng.subject.infra.basic.entity.AuthPermission;
import com.xiaofeng.subject.infra.basic.entity.AuthRole;
import com.xiaofeng.subject.infra.basic.service.AuthPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chc
 * @Date: 2024/02/21/11:48
 * @Description:
 */
@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        //将AuthPermissionBO转化为AuthPermission
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        //设置为未删除状态
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //保存到数据库
        Integer count = authPermissionService.insert(authPermission);
        return count > 0;
    }

    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }

    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = new AuthPermission();
        authPermission.setId(authPermissionBO.getId());
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }

    @Override
    public List<String> getPermission(String userName) {
        //构建缓存关键字key
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        //根据关键字key获取redis中的权限缓存
        String permissionValue = redisUtil.get(permissionKey);
        if (StringUtils.isBlank(permissionValue)) {
            return Collections.emptyList();
        }
        //将redis中的数据反序列化
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue,new TypeToken<List<AuthPermission>>(){
        }.getType());
        //根据权限关键字遍历循环获取value
        List<String> authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }
}
